package com.abe.jetpack.inject.module

import android.content.Context
import com.abe.jetpack.base.ManagerAPP
import com.abe.jetpack.helper.Constant
import com.abe.jetpack.helper.JsonHelper
import com.abe.jetpack.model.remote.wanAndroidService
import com.abe.jetpack.model.remote.ydzfYfwService
import com.abe.jetpack.model.room.AppDatabase
import com.abe.jetpack.model.room.AuthorDao
import com.abe.libcore.base.http.RetrofitUtils
import com.abe.libcore.utils.SPUtils
import dagger.Module
import dagger.Provides
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import javax.inject.Singleton

@Module
class AppModule(private val application: ManagerAPP) {

    @Singleton
    @Provides
    fun application(): ManagerAPP = application

    @Singleton
    @Provides
    fun context(): Context = application.applicationContext

    @Singleton
    @Provides
    fun spUtils(context: Context): SPUtils = SPUtils(context, Constant.APP_SP)

    @Singleton
    @Provides
    fun appRetrofit(): Retrofit {
        return RetrofitUtils
                .getJsonRetrofit(application,
                        Constant.moreBaseUrlEntity,
                        JsonHelper.gson(JsonHelper.JSON_NORMAL))
                //添加RxAndroid支持
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .baseUrl(Constant.WanAndroid)
                .build()
    }

    @Singleton
    @Provides
    fun appDataBase(): AppDatabase = AppDatabase.getInstance(application.applicationContext)

    @Singleton
    @Provides
    fun wanAndroidService(retrofit: Retrofit): wanAndroidService = retrofit.create(wanAndroidService::class.java)

    @Singleton
    @Provides
    fun authorDao(database: AppDatabase): AuthorDao = database.authorDao()

    @Singleton
    @Provides
    fun ydzfService(retrofit: Retrofit): ydzfYfwService = retrofit.create(ydzfYfwService::class.java)
}